﻿Public Class frmMain
    Const sVersion As String = "2.0.52"
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim bolError As Boolean = False
        Dim sError As String = "INI File Loaded"
        Dim INI_Settings As New INI_Class
        Me.Text = Me.Text & " ver " & sVersion


        'OLD METHOD OF GETTING INI LOCATION
        'CFG_File = CurDir() & "\CFG.INI"
        'modMain.Proc_CFG(bolError, _
        '                 sError, _
        '                 CFG_File, _
        '                 INI_File)


        'Retrieve the location of the ini file
        INI_File = ConfigHelper.GetInitialIniLocation()

        Me.tbINI_File.Text = INI_File

        modMain.Proc_INI(bolError, _
                         sError, _
                         INI_Settings)
        Me.lbError.Text = sError
        With INI_Settings
            Me.tbCampaign.Text = .sCampaign
            Me.tbServer.Text = .sServer
            Me.tbData.Text = .sData
            Me.tbEventlog.Text = .sINIDate

        End With

    End Sub

    
    Private Sub Set_INI_Data(ByRef INI_Settings As INI_Class)
        With INI_Settings
            .sCampaign = Me.tbCampaign.Text
            .sServer = Me.tbServer.Text
            .sData = Me.tbData.Text
            .sINIDate = Me.tbEventlog.Text
            
        End With
    End Sub

    

    Private Sub butGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butGenerate.Click
        Dim INI_Settings As New INI_Class
        Dim bolError As Boolean
        Dim sError As String = "Mission Generated"
        Me.butGenerate.Enabled = False
        Me.lbError.Text = "Working!"
        Set_INI_Data(INI_Settings)
        modMain.Find_Route_Folder(bolError, _
                                  sError, _
                                  INI_Settings.sCampaign, _
                                  INI_Settings.sMainFolder)
        If Not bolError Then modMain.Main("", _
                                            sError, _
                                            INI_Settings)
        If Not bolError Then modMain.GenINIFile(bolError, _
                                                sError, _
                                                INI_Settings)
        Me.tbEventlog.Text = INI_Settings.sINIDate
        Me.lbError.Text = sError
        Me.butGenerate.Enabled = True
    End Sub

    Private Sub tbEventlog_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim INI_Settings As New INI_Class
        Dim bolError As Boolean
        Dim sError As String = ""
        Set_INI_Data(INI_Settings)
        modMain.GenINIFile(bolError, _
                           sError, _
                           INI_Settings)
    End Sub

    Private Sub btCampaign_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim INI_Settings As New INI_Class
        Dim bolError As Boolean
        Dim sError As String = ""
        With Me.FolderBrowserDialog1
            .SelectedPath = Me.tbCampaign.Text
            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                Me.tbCampaign.Text = .SelectedPath & "\"
                Set_INI_Data(INI_Settings)
                modMain.GenINIFile(bolError, _
                                   sError, _
                                   INI_Settings)
            End If
        End With
    End Sub

    Private Sub btServer_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim INI_Settings As New INI_Class
        Dim bolError As Boolean
        Dim sError As String = ""
        With Me.FolderBrowserDialog1
            .SelectedPath = Me.tbServer.Text
            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                Me.tbServer.Text = .SelectedPath & "\"
                Set_INI_Data(INI_Settings)
                modMain.GenINIFile(bolError, _
                                   sError, _
                                   INI_Settings)
            End If
        End With
    End Sub

    Private Sub btData_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim INI_Settings As New INI_Class
        Dim bolError As Boolean
        Dim sError As String = ""
        With Me.FolderBrowserDialog1
            .SelectedPath = Me.tbData.Text
            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                Me.tbData.Text = .SelectedPath & "\"
                Set_INI_Data(INI_Settings)
                modMain.GenINIFile(bolError, _
                                   sError, _
                                   INI_Settings)
            End If
        End With
    End Sub

    Private Sub btOpen_INI_File_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btOpen_INI_File.Click
        Dim INI_Settings As New INI_Class
        Dim bolError As Boolean
        Dim sError As String = ""
        With Me.OpenFileDialog1
            .Filter = "INI Files (*.ini)|*.ini"

            .FileName = Me.tbINI_File.Text
            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                Me.tbINI_File.Text = .FileName
                INI_File = .FileName

                'save the location of the ini file
                ConfigHelper.WriteInitialIniLocation(INI_File)

                'modMain.GenCFGFile(bolError, _
                '                   sError)

                modMain.Proc_INI(bolError, _
                         sError, _
                         INI_Settings)
                With INI_Settings
                    Me.tbCampaign.Text = .sCampaign
                    Me.tbServer.Text = .sServer
                    Me.tbData.Text = .sData
                    Me.tbEventlog.Text = .sINIDate

                End With
            End If
        End With
    End Sub
End Class
